package cn.t.repository.impl.sku;

import cn.t.converter.sku.CategoryConverter;
import cn.t.core.mode.PageQueryDTO;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.sku.CategoryDO;
import cn.t.persistence.entity.sku.CategoryPO;
import cn.t.persistence.mapper.sku.CategoryMapper;
import cn.t.repository.sku.CategoryRepository;
import cn.t.request.CategoryReq;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品类别表(Category)数据仓库
 *
 * @author t
 * @since 2024-06-11 10:35:40
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CategoryRepositoryImpl extends RepositoryImpl<CategoryMapper, CategoryPO, CategoryConverter, CategoryDO> implements CategoryRepository {


    @Override
    public List<CategoryDO> findByModel(CategoryDO categoryDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + direction + " limit " + start + "," + pageSize
                :
                " limit " + start + "," + pageSize;

        List<CategoryPO> list = getWrapper(categoryDO)
                .last(lastSql)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<CategoryPO> getWrapper(CategoryDO categoryDO) {
        LambdaQueryChainWrapper<CategoryPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(categoryDO.getCategoryName())) {
            wrapper.eq(CategoryPO::getCategoryName, categoryDO.getCategoryName());
        }
        if (!ObjectUtils.isEmpty(categoryDO.getId())) {
            wrapper.eq(CategoryPO::getId, categoryDO.getId());
        }
        if (!ObjectUtils.isEmpty(categoryDO.getParentId())) {
            wrapper.eq(CategoryPO::getParentId, categoryDO.getParentId());
        }
        if (!ObjectUtils.isEmpty(categoryDO.getGmtCreated())) {
            wrapper.between(CategoryPO::getGmtCreated, categoryDO.getGmtCreatedStart(), categoryDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(categoryDO.getGmtModified())) {
            wrapper.between(CategoryPO::getGmtModified, categoryDO.getGmtModifiedStart(), categoryDO.getGmtModifiedEnd());
        }
        return wrapper;
    }

    @Override
    public long count(CategoryDO categoryDO) {
        return getWrapper(categoryDO).count();
    }

    @Override
    public List<CategoryDO> queryCategoryByPage(PageQueryDTO<CategoryReq> req, String field, String desc) {
        LambdaQueryChainWrapper<CategoryPO> wrapper = getWrapper(req.getParam());
        String lastSql = StringUtils.hasText(field) ?
                " order by " + field +" "+ desc + " limit " + req.getStart() + "," + req.getPageSize()
                :
                " limit " + req.getStart() + "," + req.getPageSize();

        List<CategoryPO> list = wrapper.last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    @Override
    public Long queryCategoryCount(CategoryReq param) {
        LambdaQueryChainWrapper<CategoryPO> wrapper = getWrapper(param);
        return wrapper.count();
    }

    @Override
    public List<CategoryDO> findNameByIds(List<Long> ids) {
        CategoryReq param = new CategoryReq();
        param.setIds(ids);
        LambdaQueryChainWrapper<CategoryPO> wrapper = getWrapper(param);
        return wrapper.list().stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<CategoryPO> getWrapper(CategoryReq categoryReq) {
        LambdaQueryChainWrapper<CategoryPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(categoryReq.getCategoryName())) {
            wrapper.eq(CategoryPO::getCategoryName, categoryReq.getCategoryName());
        }
        if (!CollectionUtils.isEmpty(categoryReq.getIds())) {
            wrapper.in(CategoryPO::getId, categoryReq.getIds());
        }
        if (!ObjectUtils.isEmpty(categoryReq.getId())) {
            wrapper.eq(CategoryPO::getId, categoryReq.getId());
        }
        return wrapper;
    }
}
